package com.aaa.controller;


import com.aaa.util.AlipayConfig;
import com.aaa.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("pay")
public class AliPayController {
    @Resource
    OrderService orderService;



    @RequestMapping("pays")
    @ResponseBody
    public String aliPayPort(String orderId, String shopMoney, Integer uid) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl,
                AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json",
                AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        Map<String, Object> pay = new HashMap<>();
        pay.put("out_trade_no", orderId);
        pay.put("product_code", "FAST_INSTANT_TRADE_PAY");
        pay.put("total_amount", shopMoney);
        pay.put("subject", "FM");
        pay.put("body", "Iphone6 16G");
        alipayRequest.setBizContent(JSON.toJSONString(pay));
        String result = "";
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("return")
    public String Return(HttpServletRequest request) throws IOException,
            AlipayApiException {
        System.out.println(request);
        // 获去返回的数据信息
        Map<String, String> stringStringMap = AlipayConfig.signVerified(request);
        System.out.println(stringStringMap);
        /*
        支付宝验证签名
        stringStringMap：数据是否一致
        AlipayConfig.alipay_public_key:支付宝公钥
        AlipayConfig.charset：编码格式
        AlipayConfig.sign_type：签名方式
        * */
        boolean signVerified = AlipaySignature.rsaCheckV1(stringStringMap,
                AlipayConfig.alipay_public_key, AlipayConfig.charset,
                AlipayConfig.sign_type);//调用SDK验证签名
        //判断是否验证成功
        if (signVerified) {
            //商户订单号
            String out_trade_no = stringStringMap.get("out_trade_no");
            //支付宝交易号
            String trade_no = stringStringMap.get("trade_no");
            //付款金额
            String total_amount = stringStringMap.get("total_amount");
            //修改支付状态
            Integer rs=orderService.updState(out_trade_no);

            System.out.println("rs："+rs);
            //根据订单号查询数据库获取订单的信息等一系列操作
            if (rs>0) {
                return "redirect:http://localhost:8081/#/";
            }else {
                return "false";
            }
        } else {
            return "false";
        }
    }


    /**
     * 进行修改订单信息的状态等
     *
     * @param request
     * @throws IOException
     * @throws AlipayApiException
     */
    @RequestMapping("notifyPay")
    public void notifyPay(HttpServletRequest request) throws IOException, AlipayApiException {
        // 获去返回的数据信息
        Map<String, String> stringStringMap = AlipayConfig.signVerified(request);
        System.out.println(stringStringMap);
        /*
        支付宝验证签名
        stringStringMap：数据是否一致
        AlipayConfig.alipay_public_key:支付宝公钥
        AlipayConfig.charset：编码格式
        AlipayConfig.sign_type：签名方式
        * */
        boolean signVerified = AlipaySignature.rsaCheckV1(stringStringMap,
                AlipayConfig.alipay_public_key, AlipayConfig.charset,
                AlipayConfig.sign_type);//调用SDK验证签名
        //判断是否验证成功
        if (signVerified) {
            //商户订单号
            String out_trade_no = stringStringMap.get("out_trade_no");
            //支付宝交易号
            String trade_no = stringStringMap.get("trade_no");
            //付款金额
            String total_amount = stringStringMap.get("total_amount");
            //根据订单号查询数据库获取订单的信息等一系列操作
        }
    }
}
